package com.jxy.test;

import com.jxy.tool.ObjectInputStreamWithLoader;
import sun.misc.VM;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

public class ReadFile {
    public static void main(String[] args) throws ClassNotFoundException {
        String file_ex = "/home/googlewell/tool/zkm8/ZKM_EX.ser";
        String file_o = "/home/googlewell/tool/zkm8/ZKM_O.ser";
        String file_so = "/home/googlewell/tool/zkm8/ZKM_SO.ser";

        readField_o(readObject(file_o));//zkm运行之后产生
        System.out.println("----------------------------");
        readField_ex(readObject(file_ex));//点击混淆之后，第一个窗口点击Next之后产生
        System.out.println("----------------------------");
        readField_so(readObject(file_so));//点击混淆之后，第二个窗口点击Ok之后产生
    }

    public static void readField_so(Object object) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().equals("F")) {
                    System.out.println(field.toString() + ":");
                    readField_zy(field.get(object));
                } else if (field.getName().equals("g")) {
                    Object[] os = (Object[]) field.get(object);
                    System.out.println(field.toString() + ":");
                    if (os != null) {
                        for (Object tmp : os) {
                            readField(tmp);
                        }
                    } else {
                        System.out.println("null");
                    }
                } else {
                    System.out.println(field.toString() + ":" + field.get(object));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void readField_ex(Object object) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().equals("m") || field.getName().equals("n")) {
                    readField(field.get(object));
                } else {
                    System.out.println(field.toString() + ":" + field.get(object));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void readField_o(Object object) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().equals("m") || field.getName().equals("n")) {
                    readField(field.get(object));
                } else {
                    System.out.println(field.toString() + ":" + field.get(object));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static ClassLoader getLoader() {
        try {
            String jar = "/home/googlewell/tool/zkm8/ZKM.jar";
            URL[] urls = new URL[]{new File(jar).toURI().toURL()};
            return new URLClassLoader(urls);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return VM.latestUserDefinedLoader();
    }

    public static Object readObject(String filePath) {
        Object object = null;
        try {
            ObjectInputStreamWithLoader oisl = new ObjectInputStreamWithLoader(new FileInputStream(filePath), getLoader());
            object = oisl.readObject();
            oisl.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static void readFile(String filePath) {
        try {
            ObjectInputStreamWithLoader oisl = new ObjectInputStreamWithLoader(new FileInputStream(filePath), getLoader());
            readField(oisl.readObject());
            oisl.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void readField(Object object) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                System.out.println(field.toString() + ":" + field.get(object));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void readField_zy(Object object) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().equals("eb")||field.getName().equals("db")) {
                    Object[] os = (Object[]) field.get(object);
                    System.out.println(field.toString() + ":");
                    if (os != null) {
                        System.out.println(Arrays.deepToString(os));
                    }
                } else {
                    System.out.println(field.toString() + ":" + field.get(object));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
